-- Viral Resistance - T-Engine 4 Module
-- Copyright (C) 2010 Mikolai Fajer
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.
--
-- Mikolai Fajer "yufra"
-- mfajer@gmail.com

require "engine.class"
require "engine.Object"
require "engine.interface.ObjectActivable"
require "engine.interface.ObjectIdentify"
require "engine.Chat"

module(..., package.seeall, class.inherit(
	engine.Object,
	engine.interface.ObjectActivable,
	engine.interface.ObjectIdentify
))

_M.__position_aware = true

function _M:init(t, no_default)
	t.encumber = t.encumber or 0

	engine.Object.init(self, t, no_default)
	engine.interface.ObjectActivable.init(self, t)
	engine.interface.ObjectIdentify.init(self, t)
end

--- Use the object (quaff, read, ...)
function _M:use(who, typ)
	local types = {}
	if self:canUseObject() then types[#types+1] = "use" end

	if not typ and #types == 1 then typ = types[1] end

	if typ == "use" then
		who:useEnergy()
		if self.use_sound then game:playSoundNear(who, self.use_sound) end
		return self:useObject(who)
	end
end

--- Returns a tooltip for the object
function _M:tooltip()
	return tostring(self:getDesc{do_color=true})
end

--- Gets the full name of the object
function _M:getName(t)
	t = t or {}
	local qty = self:getNumber()
	local name = self.name

	-- To extend later
	name = name:gsub("~", ""):gsub("&", "a"):gsub("#([^#]+)#", function(attr)
		return self:descAttribute(attr)
	end)

	if not t.do_color then
		if qty == 1 or t.no_count then return name
		else return qty.." "..name
		end
	else
		local c = "#B4B4B4#"
		if qty == 1 or t.no_count then return c..name.."#LAST#"
		else return c..qty.." "..name.."#LAST#"
		end
	end
end

--- Gets the full desc of the object
function _M:getDesc()
	local desc = tstring{}
	desc:add({"color", 255, 255, 255}, self:getName(), {"color", "LAST"}, true)
	if self.desc then
		desc:add(true)
		desc:merge(self.desc)
	end

	local reqs = self:getRequirementDesc(game.player)
	if reqs then
		desc:add(true)
		desc:merge(reqs)
	end

	if self.quantity then
		desc:add({("Quantity: %d"):format(self.quantity), true})
	end

	if self.encumber then
		desc:add({"color",0x67,0xAD,0x00}, ("%0.2f Encumbrance."):format(self.encumber), {"color", "LAST"}, true)
	end

	desc:add(("Type: %s / %s"):format(self.type, self.subtype), true)

	return desc
end

function _M:block_move(x, y, e, act)
	-- Talking?
	if self.can_talk and act and e.player then
		local target = game.level.map(x, y, engine.Map.TERRAIN)
		local chat = engine.Chat.new(self.can_talk, self, e)
		chat:invoke()
	end
	
	return self.does_block_move
end